package com.work.dyduser.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.work.dyduser.bean.ShoppingCart;

import java.util.List;

/**
 * author：WangZhan
 * data: 2016/12/5 09:16
 * version: 1.0.0
 * explain:购物车分类型适配器
 */

public class GWCRecyclerViewAdapter extends RecyclerView.Adapter {
    private CheckBox shopping_checkbox;
    private Context context;
    private List<ShoppingCart> list;
    private List<String> list2;
    private CheckBox cb_gwc;

    public GWCRecyclerViewAdapter(Context context, List<ShoppingCart> list, List<String> list2, final CheckBox shopping_checkbox) {
        this.context = context;
        this.list = list;
        this.list2 = list2;
        this.shopping_checkbox=shopping_checkbox;
        //设置CheckBox的点击事件
        shopping_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //1. 得到状态，如果是选中，就把所有数据设置勾选；如果状态是非勾选，设置全部为不选择
                all_none(shopping_checkbox.isChecked());

                //2.显示总价格
//                showTotalPrice();
            }
        });
    }
    /**
     * 全选和非全选
     *
     * @param ischeck
     */
    public void all_none(boolean ischeck) {
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                ShoppingCart cart = list.get(i);
                cart.setIsCheck(ischeck);
            }
            notifyDataSetChanged();

            if (list.size() > 0) {
                shopping_checkbox.setVisibility(View.VISIBLE);
            } else {
                shopping_checkbox.setVisibility(View.GONE);
            }
        }
    }
    /**
     * 全选和非全选
     */
    public void checkAll_none() {
        if (list != null && list.size() > 0) {
            int number = 0;
            for (int i = 0; i < list.size(); i++) {
                ShoppingCart cart = list.get(i);
                if (!cart.isCheck()) {//只要有一个不被选中，就设置全选为非勾选状态
                    //没选择的
                    shopping_checkbox.setChecked(false);
                } else {
                    //选中的
                    number += 1;
                }
            }

            if (number == list.size()) {
                shopping_checkbox.setChecked(true);
            }

//                if(datas.size()>0){
//                    checkbox_all.setVisibility(View.VISIBLE);
//                }else{
//                    checkbox_all.setVisibility(View.GONE);
//                }

        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
//            View view = LayoutInflater.from(context).inflate(R.layout.item_gwc_viewtype1, parent, false);
//            return new ViewHolder1(view);
        } else if (viewType == 1) {
//            View view = LayoutInflater.from(context).inflate(R.layout.item_gwc_viewtype2, parent, false);
//            return new ViewHolder2(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        //recyclerview的点击事件
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView, position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
       return position;
    }

    private class ViewHolder1 extends RecyclerView.ViewHolder {

        public ViewHolder1(View itemView) {
            super(itemView);
            setData(0);
        }

        public void setData(int position) {
//            listview.setAdapter(new CommonAdapter<ShoppingCart>(context,list,R.layout.item_gwc_viewtype1_item) {
//                public LinearLayout lt_cb;
//
//                @Override
//                public void convert(ViewHolder helper, final ShoppingCart item, final int position) {
//                    final ShoppingCart cart = list.get(position);
//                    final boolean check = cart.isCheck();
//                    cb_gwc=helper.getView(R.id.cb_gwc);
//                    lt_cb=helper.getView(R.id.lt_cb);
//                    cb_gwc.setChecked(check);
//                    lt_cb.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            LogUtils.d("点击的第几个"+position);
//                            cart.setIsCheck(!check);
//                            checkAll_none();
//                            notifyDataSetChanged();
//                        }
//                    });
//                }
//            });
        }
    }

    private class ViewHolder2 extends RecyclerView.ViewHolder {

        public ViewHolder2(View itemView) {
            super(itemView);

            setData(1);
        }

        public void setData(int position) {


        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}
